﻿#include "TlvTagCheckListWidget.h"
#include "stores/DataStroes.h"

TlvTagCheckListWidget::TlvTagCheckListWidget(QWidget *parent)
    : CheckableListWidget{parent}
{
    const QList<TlvTag>& filters = DataStroes::instance()->settings()->model().data().log.tag_filter;

    for (const TlvTag& tag : Tlv::allTags()) {
        QString tagStr = Tlv::tagStr(tag);
        bool tagCheck = false;
        QString label = QString("0x%1 (%2)").arg(QString::number(tag, 16), 4, '0').arg(tagStr);
        for (const TlvTag& ti : filters) {
            if (ti == tag) {
                tagCheck = true;
                break;
            }
        }
        addItem(label, tagCheck);
    }
}

QList<TlvTag> TlvTagCheckListWidget::toLogTagFilterModel() const
{
    QList<TlvTag>& filters = DataStroes::instance()->settings()->model().data().log.tag_filter;
    filters.clear();

    QList<TlvTag> tags;
    for (int i = 0; i < count(); i++) {
        if (isItemChecked(i)) {
            TlvTag tag = Tlv::allTags().at(i);
            tags.push_back(tag);
            filters.push_back(tag);
        }
    }
    return tags;
}
